<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/3">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/10. Zabbix监控字符集问题/">Zabbix监控字符集问题</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix监控中常碰到的字符集问题有三个：</p>
<h2 id="一、图形中显示中文乱码"><a href="#一、图形中显示中文乱码" class="headerlink" title="一、图形中显示中文乱码"></a>一、图形中显示中文乱码</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E7%9B%91%E6%8E%A7%E5%AD%97%E7%AC%A6%E9%9B%86%E9%97%AE%E9%A2%98/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决方法：</p>
<ol>
<li><p>从window主机路径C:\Windows\Fonts下选择一种自己喜欢的字体，如mysh.ttf(雅黑)；</p>
</li>
<li><p>将拷贝出来的字体上传到zabbix server网页文件的fonts目录下，并命名为<strong>DejaVuSans.ttf</strong></p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># mv /var/www/html/fonts/DejaVuSans.ttf /var/www/html/fonts/DejaVuSans.ttf.old</span></div><div class="line">mv /var/www/html/fonts/mysh.ttf /var/www/html/fonts/DejaVuSans.ttf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样便能解决这个乱码问题</p>
<h2 id="二、历史记录处出现问号乱码"><a href="#二、历史记录处出现问号乱码" class="headerlink" title="二、历史记录处出现问号乱码"></a>二、历史记录处出现问号乱码</h2><p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E7%9B%91%E6%8E%A7%E5%AD%97%E7%AC%A6%E9%9B%86%E9%97%AE%E9%A2%98/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这与zabbix数据库所使用的字符集有关，解决方法如下：</p>
<ol>
<li>查看当前的默认字符集</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create database zabbix;</div><div class="line">+----------+-------------------------------------------------------------------+</div><div class="line">| Database | Create Database                                                   |</div><div class="line">+----------+-------------------------------------------------------------------+</div><div class="line">| zabbix   | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET latin1 */ |</div><div class="line">+----------+-------------------------------------------------------------------+</div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div><div class="line"><span class="comment">#安装时在导入三个sql文件时，zabbix数据库中创建的表的字符集也是latin1</span></div><div class="line">mysql&gt; show variables like <span class="string">'character%'</span>;</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| Variable_name            | Value                      |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| character_set_client     | latin1                     |</div><div class="line">| character_set_connection | latin1                     |</div><div class="line">| character_set_database   | latin1                     |</div><div class="line">| character_set_filesystem | binary                     |</div><div class="line">| character_set_results    | latin1                     |</div><div class="line">| character_set_server     | latin1                     |</div><div class="line">| character_set_system     | utf8                       |</div><div class="line">| character_sets_dir       | /usr/share/mysql/charsets/ |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">8 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<ol>
<li>这是由于字符集不是utf8引起的，现在的解决方法有两种：</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当当前zabbix数据库中已存在一定的数据的解决办法</p>
<ul>
<li>备份zabbix数据库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># mysqldump -uroot -p123456 zabbix &gt; zabbix.sql</span></div></pre></td></tr></table></figure>
<ul>
<li>修改备份文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># sed -i 's/latin1/utf8/g' zabbix.sql</span></div></pre></td></tr></table></figure>
<ul>
<li>删除zabbix数据库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; drop database zabbix;</div></pre></td></tr></table></figure>
<ul>
<li>关闭mysql数据库，设置默认字符集</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># vim /etc/my.cnf</span></div><div class="line">[mysqld]</div><div class="line"><span class="built_in">log</span>-bin</div><div class="line">datadir=/var/lib/mysql</div><div class="line">socket=/var/lib/mysql/mysql.sock</div><div class="line">default-character-set = utf8 <span class="comment">#添加该项</span></div></pre></td></tr></table></figure>
<ul>
<li>启动mysql并恢复zabbix数据库</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@zhu1 ~]<span class="comment"># mysql -uroot -p123456 zabbix &lt; zabbix.sql</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create database zabbix;</div><div class="line">+----------+-----------------------------------------------------------------+</div><div class="line">| Database | Create Database                                                 |</div><div class="line">+----------+-----------------------------------------------------------------+</div><div class="line">| zabbix   | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET utf8 */ |</div><div class="line">+----------+-----------------------------------------------------------------+</div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div><div class="line">mysql&gt; show variables like <span class="string">'character%'</span>;</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| Variable_name            | Value                      |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">| character_set_client     | latin1                     |</div><div class="line">| character_set_connection | latin1                     |</div><div class="line">| character_set_database   | utf8                       |</div><div class="line">| character_set_filesystem | binary                     |</div><div class="line">| character_set_results    | latin1                     |</div><div class="line">| character_set_server     | utf8                       |</div><div class="line">| character_set_system     | utf8                       |</div><div class="line">| character_sets_dir       | /usr/share/mysql/charsets/ |</div><div class="line">+--------------------------+----------------------------+</div><div class="line">8 rows <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<ul>
<li>访问页面</li>
</ul>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Zabbix%E7%9B%91%E6%8E%A7%E5%AD%97%E7%AC%A6%E9%9B%86%E9%97%AE%E9%A2%98/03.png?raw=true" alt=""></figure></p>
<h2 id="当安装后发现，则可以："><a href="#当安装后发现，则可以：" class="headerlink" title="当安装后发现，则可以："></a>当安装后发现，则可以：</h2><ol>
<li>删除zabbix数据库</li>
<li>设置mysql数据库的默认字符集为utf8</li>
<li>重新创建zabbix数据库并导入三个sql文件</li>
</ol>
<h2 id="总结："><a href="#总结：" class="headerlink" title="总结："></a>总结：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">--with-charset=CHARSET</div><div class="line">Default character <span class="built_in">set</span>, use one of:</div><div class="line">binary</div><div class="line">armscii8 ascii big5 cp1250 cp1251 cp1256 cp1257</div><div class="line">cp850 cp852 cp866 cp932 dec8 eucjpms euckr gb2312 gbk geostd8</div><div class="line">greek hebrew hp8 keybcs2 koi8r koi8u</div><div class="line">latin1 latin2 latin5 latin7 macce macroman</div><div class="line">sjis swe7 tis620 ucs2 ujis utf8</div><div class="line">--with-extra-charsets=CHARSET,CHARSET,...</div><div class="line">Use charsets <span class="keyword">in</span> addition to default (none, complex,</div><div class="line">all, or a list selected from the above sets)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上面这两个是编译安装mysql事对字符集设置的参数，当不进行设置时默认便是latin1，</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Zabbix/1. 分布式系统监视 zabbix/">分布式系统监视 zabbix</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F%E7%9B%91%E8%A7%86Zabbix/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix能监视各种网络参数，保证服务器系统的安全运营；并提供柔软的通知机制以让系统管理员快速定位/解决存在的各种问题。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>zabbix</strong>由2部分构成，<strong>zabbix server</strong>与可选组件<strong>zabbix agent</strong>。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>zabbix server</strong>可以通过SNMP，<strong>zabbix agent</strong>，ping，端口监视等方法提供对远程服务器/网络状态的监视，数据收集等功能，它可以运行在Linux, Solaris, HP-UX, AIX, Free BSD, Open BSD, OS X等平台之上。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>zabbix agent</strong>需要安装在被监视的目标服务器上，它主要完成对硬件信息或与操作系统有关的内存，CPU等信息的收集。<strong>zabbix agent</strong>可以运行在Linux ,Solaris, HP-UX, AIX, Free BSD, Open BSD, OS X, Tru64/OSF1, Windows NT4.0, Windows 2000/2003/XP/Vista)等系统之上。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>zabbix server</strong>可以单独监视远程服务器的服务状态；同时也可以与<strong>zabbix agent</strong>配合，可以轮询<strong>zabbix agent</strong>主动接收监视数据（trapping方式），同时还可被动接收<strong>zabbix agent</strong>发送的数据（trapping方式）。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外<strong>zabbix server</strong>还支持SNMP (v1,v2)，可以与SNMP软件(例如：net-snmp)等配合使用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>zabbix的主要特点</strong>：</p>
<ul>
<li>安装与配置简单，学习成本低</li>
<li>支持多语言（包括中文）</li>
<li>免费开源</li>
<li>自动发现服务器与网络设备</li>
<li>分布式监视以及WEB集中管理功能</li>
<li>可以无agent监视</li>
<li>用户安全认证和柔软的授权方式</li>
<li>通过WEB界面设置或查看监视结果</li>
<li>email等通知功能 </li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;等等</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>Zabbix主要功能</strong>：</p>
<ul>
<li>CPU负荷</li>
<li>内存使用</li>
<li>磁盘使用</li>
<li>网络状况</li>
<li>端口监视</li>
<li>日志监视</li>
</ul>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F%E7%9B%91%E8%A7%86Zabbix/02.png?raw=true" alt=""></figure></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/9. tomcat 性能优化/">tomcat 性能优化</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tomcat默认参数是为开发环境制定，而非适合生产环境，尤其是内存和线程的配置，默认都很低，容易成为性能瓶颈。</p>
<h2 id="tomcat内存优化"><a href="#tomcat内存优化" class="headerlink" title="tomcat内存优化"></a>tomcat内存优化</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;linux修改TOMCAT_HOME/bin/catalina.sh，在前面加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">JAVA_OPTS=<span class="string">"-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;windows修改TOMCAT_HOME/bin/catalina.bat，在前面加入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最大堆内存是1024m，对于现在的硬件还是偏低，实施时，还是按照机器具体硬件配置优化。</p>
<h2 id="tomcat-线程优化"><a href="#tomcat-线程优化" class="headerlink" title="tomcat 线程优化"></a>tomcat 线程优化</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"80"</span> protocol=<span class="string">"HTTP/1.1"</span> maxThreads=<span class="string">"600"</span> minSpareThreads=<span class="string">"100"</span> maxSpareThreads=<span class="string">"500"</span> acceptCount=<span class="string">"700"</span></div><div class="line">connectionTimeout=<span class="string">"20000"</span> redirectPort=<span class="string">"8443"</span> /&gt;</div><div class="line">maxThreads=<span class="string">"600"</span>       ///最大线程数</div><div class="line">minSpareThreads=<span class="string">"100"</span>///初始化时创建的线程数</div><div class="line">maxSpareThreads=<span class="string">"500"</span>///一旦创建的线程超过这个值，Tomcat就会关闭不再需要的socket线程。</div><div class="line">acceptCount=<span class="string">"700"</span>//指定当所有可以使用的处理请求的线程数都被使用时，可以放到处理队列中的请求数，超过这个数的请求将不予处理</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里是http connector的优化，如果使用apache和tomcat做集群的负载均衡，并且使用ajp协议做apache和tomcat的协议转发，那么还需要优化ajp connector。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8009"</span> protocol=<span class="string">"AJP/1.3"</span> maxThreads=<span class="string">"600"</span> minSpareThreads=<span class="string">"100"</span> maxSpareThreads=<span class="string">"500"</span> acceptCount=<span class="string">"700"</span></div><div class="line">connectionTimeout=<span class="string">"20000"</span> redirectPort=<span class="string">"8443"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由于tomcat有多个connector，所以tomcat线程的配置，又支持多个connector共享一个线程池。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先。打开/conf/server.xml，增加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Executor name=<span class="string">"tomcatThreadPool"</span> namePrefix=<span class="string">"catalina-exec-"</span> maxThreads=<span class="string">"500"</span> minSpareThreads=<span class="string">"20"</span> maxIdleTime=<span class="string">"60000"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最大线程500（一般服务器足以），最小空闲线程数20，线程最大空闲时间60秒。</p>
<p>然后，修改<connector ...="">节点，增加executor属性，executor设置为线程池的名字：</connector></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Connector executor=<span class="string">"tomcatThreadPool"</span> port=<span class="string">"80"</span> protocol=<span class="string">"HTTP/1.1"</span>  connectionTimeout=<span class="string">"60000"</span> keepAliveTimeout=<span class="string">"15000"</span> maxKeepAliveRequests=<span class="string">"1"</span>  redirectPort=<span class="string">"443"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以多个connector公用1个线程池，所以ajp connector也同样可以设置使用tomcatThreadPool线程池。</p>
<h2 id="禁用DNS查询"><a href="#禁用DNS查询" class="headerlink" title="禁用DNS查询"></a>禁用DNS查询</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;当web应用程序向要记录客户端的信息时，它也会记录客户端的IP地址或者通过域名服务器查找机器名 转换为IP地址。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DNS查询需要占用网络，并且包括可能从很多很远的服务器或者不起作用的服务器上去获取对应的IP的过程，这样会消耗一定的时间。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改server.xml文件中的Connector元素，修改属性enableLookups参数值: enableLookups=”false”</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果为true，则可以通过调用request.getRemoteHost()进行DNS查询来得到远程客户端的实际主机名，若为false则不进行DNS查询，而是返回其ip地址</p>
<h2 id="设置session过期时间"><a href="#设置session过期时间" class="headerlink" title="设置session过期时间"></a>设置session过期时间</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;conf\web.xml中通过参数指定：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;session-config&gt;   </div><div class="line">    &lt;session-timeout&gt;180&lt;/session-timeout&gt;     </div><div class="line">&lt;/session-config&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;单位为分钟。</p>
<h2 id="Apr插件提高Tomcat性能"><a href="#Apr插件提高Tomcat性能" class="headerlink" title="Apr插件提高Tomcat性能"></a>Apr插件提高Tomcat性能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat可以使用APR来提供超强的可伸缩性和性能，更好地集成本地服务器技术.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;APR(Apache Portable Runtime)是一个高可移植库，它是Apache HTTP Server 2.x的核心。APR有很多用途，包括访问高级IO功能(例如sendfile,epoll和OpenSSL)，OS级别功能(随机数生成，系统状态等等)，本地进程管理(共享内存，NT管道和UNIX sockets)。这些功能可以使Tomcat作为一个通常的前台WEB服务器，能更好地和其它本地web技术集成，总体上让Java更有效率作为一个高性能web服务器平台而不是简单作为后台容器。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在产品环境中，特别是直接使用Tomcat做WEB服务器的时候，应该使用Tomcat Native来提高其性能  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要测APR给tomcat带来的好处最好的方法是在慢速网络上（模拟Internet），将Tomcat线程数开到300以上的水平，然后模拟一大堆并发请求。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不配APR，基本上300个线程狠快就会用满，以后的请求就只好等待。但是配上APR之后，并发的线程数量明显下降，从原来的300可能会马上下降到只有几十，新的请求会毫无阻塞的进来。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在局域网环境测，就算是400个并发，也是一瞬间就处理/传输完毕，但是在真实的Internet环境下，页面处理时间只占0.1%都不到，绝大部分时间都用来页面传输。如果不用APR，一个线程同一时间只能处理一个用户，势必会造成阻塞。所以生产环境下用apr是非常必要的。</p>
<ol>
<li>安装APR tomcat-native</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;apr-1.3.8.tar.gz   安装在/usr/local/apr</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">tar zxvf apr-1.3.8.tar.gz</div><div class="line"><span class="built_in">cd</span> apr-1.3.8</div><div class="line">./configure;make;make install</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;apr-util-1.3.9.tar.gz  安装在/usr/local/apr/lib</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">tar zxvf apr-util-1.3.9.tar.gz</div><div class="line"><span class="built_in">cd</span> apr-util-1.3.9  </div><div class="line">./configure --with-apr=/usr/<span class="built_in">local</span>/apr ----with-java-home=JDK;make;make install</div><div class="line"></div><div class="line"><span class="built_in">cd</span> apache-tomcat-6.0.20/bin  </div><div class="line">tar zxvf tomcat-native.tar.gz  </div><div class="line"><span class="built_in">cd</span> tomcat-native/jni/native  </div><div class="line">./configure --with-apr=/usr/<span class="built_in">local</span>/apr;make;make install</div></pre></td></tr></table></figure>
<ol>
<li>设置 Tomcat 整合 APR</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改 tomcat 的启动 shell （startup.sh），在该文件中加入启动参数：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">CATALINA_OPTS=<span class="string">"<span class="variable">$CATALINA_OPTS</span> -Djava.library.path=/usr/local/apr/lib"</span> 。</div></pre></td></tr></table></figure>
<ol>
<li>判断安装成功:</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果看到下面的启动日志，表示成功。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">2007-4-26 15:34:32 org.apache.coyote.http11.Http11AprProtocol init</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/8. tomcat 的优化配置/">tomcat 的优化配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<h2 id="一、精简Tomcat和配置文件"><a href="#一、精简Tomcat和配置文件" class="headerlink" title="一、精简Tomcat和配置文件"></a>一、精简Tomcat和配置文件</h2><p>1．删除不需要的管理应用和帮助应用，提高tomcat安全性。</p>
<ul>
<li>删除webapps下所有文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm –fr <span class="variable">$CATALINA_HOME</span>/webapps/*</div></pre></td></tr></table></figure>
<ul>
<li>删除server/wenapps下所有文件</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rm –fr <span class="variable">$CATALINA_HOME</span>/server/webapps/*</div></pre></td></tr></table></figure>
<p>2．精简sever.xml配置文件</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用tomcat发布版本中的最小配置文件，提高性能，如果有功能上的需求，在逐个的加入功能配置。</p>
<ul>
<li>备份原来的server.xml为server.xml_bak</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mv server.xml server.xml_bak</div></pre></td></tr></table></figure>
<ul>
<li>复制server-minimal.xml为server.xml</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">cp server-minimal.xml server.xml</div></pre></td></tr></table></figure>
<h2 id="二、连接器优化"><a href="#二、连接器优化" class="headerlink" title="二、连接器优化"></a>二、连接器优化</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在$CATALINA_HOME/conf/server.xml配置文件中的Connetctor节点，和连接数相关的参数配置和优化。</p>
<h3 id="maxThreads"><a href="#maxThreads" class="headerlink" title="maxThreads"></a>maxThreads</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。默认值200。 可以根据机器的时期性能和内存大小调整，一般可以在400-500。最大可以在800左右。</p>
<h3 id="acceptCount"><a href="#acceptCount" class="headerlink" title="acceptCount"></a>acceptCount</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定当所有可以使用的处理请求的线程数都被使用时，可以放到处理队列中的请求数，超过这个数的请求将不予处理。默认值10。 </p>
<h3 id="minSpareThreads"><a href="#minSpareThreads" class="headerlink" title="minSpareThreads"></a>minSpareThreads</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat初始化时创建的线程数。默认值4。 </p>
<h3 id="maxSpareThreads"><a href="#maxSpareThreads" class="headerlink" title="maxSpareThreads"></a>maxSpareThreads</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一旦创建的线程超过这个值，Tomcat就会关闭不再需要的socket线程。默认值50。 </p>
<h3 id="enableLookups"><a href="#enableLookups" class="headerlink" title="enableLookups"></a>enableLookups</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;是否反查域名，默认值为true。为了提高处理能力，应设置为false </p>
<h3 id="connnectionTimeout"><a href="#connnectionTimeout" class="headerlink" title="connnectionTimeout"></a>connnectionTimeout</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;网络连接超时，默认值20000，单位：毫秒。设置为0表示永不超时，这样设置有隐患的。通常可设置为30000毫秒。(本系统由于与后台系统接口超时时间较长，使用设置为60000) </p>
<h3 id="maxKeepAliveRequests"><a href="#maxKeepAliveRequests" class="headerlink" title="maxKeepAliveRequests"></a>maxKeepAliveRequests</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保持请求数量，默认值100。 </p>
<h3 id="bufferSize"><a href="#bufferSize" class="headerlink" title="bufferSize"></a>bufferSize</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;输入流缓冲大小，默认值2048 bytes。 </p>
<h3 id="compression"><a href="#compression" class="headerlink" title="compression"></a>compression</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;压缩传输，取值on/off/force，默认值off。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中和最大连接数相关的参数为maxThreads和acceptCount。如果要加大并发连接数，应同时加大这两个参数。web server允许的最大连接数还受制于操作系统的内核参数设置，通常Windows是2000个左右，Linux是1000个左右。 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat中如何禁止和允许列目录下的文件 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在$CATALINA_HOME/conf/web.xml中，把listings参数设置成false即可，如下： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">listings</div><div class="line"><span class="literal">false</span></div><div class="line">...</div></pre></td></tr></table></figure>
<h3 id="具体操作"><a href="#具体操作" class="headerlink" title="具体操作"></a>具体操作</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi <span class="variable">$CATALINA_HOME</span>/conf/server.xml</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改用于AJP的连接：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8009"</span> protocol=<span class="string">"AJP/1.3"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8009"</span></div><div class="line">               maxTreads=<span class="string">"500"</span> minSpareThreads=<span class="string">"10"</span> maxSpareThreads=<span class="string">"50"</span></div><div class="line">               acceptCount=<span class="string">"50"</span> connectionTimeout=<span class="string">"60000"</span></div><div class="line">               enableLookups=<span class="string">"false"</span> redirectPort=<span class="string">"8443"</span> protocol=<span class="string">"AJP/1.3"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改通用连接：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8080"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;Connector port=<span class="string">"8080"</span></div><div class="line">               maxTreads=<span class="string">"500"</span> minSpareThreads=<span class="string">"10"</span> maxSpareThreads=<span class="string">"50"</span></div><div class="line">               acceptCount=<span class="string">"50"</span> connectionTimeout=<span class="string">"60000"</span></div><div class="line">               enableLookups=<span class="string">"false"</span> redirectPort=<span class="string">"8443"</span> protocol=<span class="string">"AJP/1.3"</span></div><div class="line">           compression=<span class="string">"on"</span></div><div class="line">           compressionMinSize=<span class="string">"2048"</span></div><div class="line">           noCompressionUserAgents=<span class="string">"gozilla, traviata"</span></div><div class="line">           compressableMimeType=<span class="string">"text/html,text/xml"</span>/&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改主机和应用配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Host name=<span class="string">"localhost"</span> appBase=<span class="string">"webapps"</span> /&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;Host name=<span class="string">"localhost"</span> appBase=<span class="string">" "</span></div><div class="line">       unpackWARs=<span class="string">"true"</span> autoDeploy=<span class="string">"true"</span></div><div class="line">       xmlValidation=<span class="string">"false"</span> xmlNamespaceAware=<span class="string">"false"</span>&gt;</div><div class="line">        &lt;Context path=<span class="string">""</span> docBase=<span class="string">"/www/xxxx/site/web"</span> reloadable=<span class="string">"true"</span> debug=<span class="string">"0"</span>/&gt;</div><div class="line">      &lt;/Host&gt;</div></pre></td></tr></table></figure>
<h2 id="三、优化JDK"><a href="#三、优化JDK" class="headerlink" title="三、优化JDK"></a>三、优化JDK</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Tomcat默认可以使用的内存为128MB,Windows下,在文件{tomcat_home}/bin/catalina.bat，Unix下，在文件$CATALINA_HOME/bin/catalina.sh的前面，增加如下设置： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">JAVA_OPTS=<span class="string">'$JAVA_OPTS -Xms[初始化内存大小] -Xmx[可以使用的最大内存]</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或设置环境变量：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_OPTS=”<span class="variable">$JAVA_OPTS</span> -Xms[初始化内存大小] -Xmx[可以使用的最大内存]”</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一般说来，你应该使用物理内存的 80% 作为堆大小。如果本机上有Apache服务器，可以先折算Apache需要的内存，然后修改堆大小。建议设置为70％；建议设置[[初始化内存大小]等于[可以使用的最大内存]，这样可以减少平凡分配堆而降低性能。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本例使用加入环境变量的方式：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi /etc/profile</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">export</span> JAVA_OPTS=”<span class="variable">$JAVA_OPTS</span> -Xms700 –Xmx700</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">source</span> /etc/profile</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/7. Nginx + Tomcat 配置负载均衡集群/">Nginx + Tomcat 配置负载均衡集群</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Nginx/">Nginx</a> <a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<h2 id="一、Hello-world"><a href="#一、Hello-world" class="headerlink" title="一、Hello world"></a>一、Hello world</h2><h3 id="前期环境准备"><a href="#前期环境准备" class="headerlink" title="前期环境准备"></a>前期环境准备</h3><ol>
<li><p>准备两个解压版tomcat，如何同时启动两个tomcat，请看我的另一篇文章《一台机器同时启动多个tomcat》。</p>
</li>
<li><p>nginx官网下载解压版nginx。</p>
</li>
<li><p>创建一个简单的web项目。为了直观的区分访问的哪个tomcat，在页面写上标记8081、8082。</p>
</li>
</ol>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/01.png?raw=true" alt=""></figure></p>
<ol>
<li>分别部署到对应的tomcat下。如图：</li>
</ol>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/02.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/03.png?raw=true" alt=""></figure></p>
<h3 id="配置nginx"><a href="#配置nginx" class="headerlink" title="配置nginx"></a>配置nginx</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进入nginx-1.10.1conf路径，修改配置文件nginx.conf。</p>
<ol>
<li>配置服务器组，在http{}节点之间添加upstream配置。（注意不要写localhost，不然访问速度会很慢）</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">server 127.0.0.1:8081;   <span class="comment">#服务器地址1</span></div><div class="line">server 127.0.0.1:8082;   <span class="comment">#服务器地址2</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>修改nginx监听的端口号80，改为8080。</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">server &#123;</div><div class="line">    listen       8080;</div><div class="line">    ......</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ol>
<li>在location{}中，利用proxy_pass配置反向代理地址；此处“<a href="http://”不能少，后面的地址要和第一步upstream定义的名称保持一致。" target="_blank" rel="external">http://”不能少，后面的地址要和第一步upstream定义的名称保持一致。</a></li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">location / &#123;</div><div class="line">root   html;</div><div class="line">index  index.html index.htm;</div><div class="line">proxy_pass http://nginxDemo; <span class="comment">#配置方向代理地址</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如下图：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/04.png?raw=true" alt=""></figure></p>
<h3 id="启动nginx和tomcat，访问"><a href="#启动nginx和tomcat，访问" class="headerlink" title="启动nginx和tomcat，访问"></a>启动nginx和tomcat，访问</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;我是Windows系统，所以直接在nginx-1.10.1目录下双击nginx.exe即可。<br>可在任务管理器中查看</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/05.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后在浏览器输入地址：<a href="http://localhost:8080/nginxDemo/index.jsp，每次访问就会轮流访问tomcat了（如果F5刷新不管用，建议试试鼠标指针放到地址栏，点击Enter键）。" target="_blank" rel="external">http://localhost:8080/nginxDemo/index.jsp，每次访问就会轮流访问tomcat了（如果F5刷新不管用，建议试试鼠标指针放到地址栏，点击Enter键）。</a></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/06.png?raw=true" alt=""></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Nginx%20+%20Tomcat%20%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E9%9B%86%E7%BE%A4/07.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;到这里，一个非常简单的负载均衡就配置完成了，是不是很简单呢，O(∩_∩)O哈哈~</p>
<h2 id="二、nginx负载均衡策略"><a href="#二、nginx负载均衡策略" class="headerlink" title="二、nginx负载均衡策略"></a>二、nginx负载均衡策略</h2><h3 id="轮询（默认）"><a href="#轮询（默认）" class="headerlink" title="轮询（默认）"></a>轮询（默认）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个web请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器down掉，能自动剔除。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081;</div><div class="line">      server 127.0.0.1:8082;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="最少链接"><a href="#最少链接" class="headerlink" title="最少链接"></a>最少链接</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;web请求会被转发到连接数最少的服务器上。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      least_conn;</div><div class="line">      server 127.0.0.1:8081;</div><div class="line">      server 127.0.0.1:8082;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="weight-权重"><a href="#weight-权重" class="headerlink" title="weight 权重"></a>weight 权重</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;指定轮询几率，weight和访问比率成正比，用于后端服务器性能不均的情况，weight默认是1。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;服务器A和服务器B的访问比例为：2-1;比如有3个请求，前两个会访问A，三个访问B，其它规则和轮询一样。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081 weight=2; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="ip-hash"><a href="#ip-hash" class="headerlink" title="ip_hash"></a>ip_hash</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个请求按访问ip的hash值分配，这样同一客户端连续的Web请求都会被分发到同一服务器进行处理，可以解决session的问题。当后台服务器宕机时，会自动跳转到其它服务器。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      ip_hash;</div><div class="line">      server 127.0.0.1:8081 weight=2; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;基于weight的负载均衡和基于ip_hash的负载均衡可以组合在一起使用。</p>
<h3 id="url-hash（第三方）"><a href="#url-hash（第三方）" class="headerlink" title="url_hash（第三方）"></a>url_hash（第三方）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;url_hash是nginx的第三方模块，nginx本身不支持，需要打补丁。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nginx按访问url的hash结果来分配请求，使每个url定向到同一个后端服务器，后端服务器为缓存服务器、文件服务器、静态服务器时比较有效。缺点是当后端服务器宕机的时候，url_hash不会自动跳转的其他缓存服务器，而是返回给用户一个503错误。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">      <span class="built_in">hash</span> <span class="variable">$request_url</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="fair（第三方）"><a href="#fair（第三方）" class="headerlink" title="fair（第三方）"></a>fair（第三方）</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;按后端服务器的响应时间来分配请求，响应时间短的优先分配。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">upstream nginxDemo &#123;</div><div class="line">      server 127.0.0.1:8081; <span class="comment">#服务器A</span></div><div class="line">      server 127.0.0.1:8082; <span class="comment">#服务器B</span></div><div class="line">      fair;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/6.  tomcat配置文件server.xml详解/">tomcat配置文件server.xml详解</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/tomcat%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6server.xml%E8%AF%A6%E8%A7%A3/01.png?raw=true" alt=""></figure></p>
<h2 id="元素"><a href="#元素" class="headerlink" title="元素"></a><server>元素</server></h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它代表整个容器,是Tomcat实例的顶层元素.由org.apache.catalina.Server接口来定义.它包含一个<service>元素.并且它不能做为任何元素的子元素.</service></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt; Server port =<span class="string">"8005"</span> shutdown =<span class="string">"SHUTDOWN"</span> debug =<span class="string">"0"</span> &gt;</div></pre></td></tr></table></figure>
<ul>
<li>className指定实现org.apache.catalina.Server接口的类.默认值为<br>org.apache.catalina.core.StandardServer</li>
<li>port指定Tomcat监听shutdown命令端口.终止服务器运行时,必须在Tomcat服务器所在的机器上发出shutdown命令.该属性是必须的.</li>
<li>shutdown指定终止Tomcat服务器运行时,发给Tomcat服务器的shutdown监听端口的字符串.该属性必须设置</li>
</ul>
<h2 id="元素-1"><a href="#元素-1" class="headerlink" title="元素"></a><service>元素</service></h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该元素由org.apache.catalina.Service接口定义,它包含一个<engine>元素,以及一个或多个<connector>,这些Connector元素共享用同一个Engine元素</connector></engine></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt; Service name =<span class="string">"Catalina"</span> &gt; </div><div class="line">    &lt; Service name =<span class="string">"Apache"</span> &gt;</div></pre></td></tr></table></figure>
<ul>
<li>第一个<service>处理所有直接由Tomcat服务器接收的web客户请求.</service></li>
<li>第二个<service>处理所有由Apahce服务器转发过来的Web客户请求<ul>
<li>className 指定实现org.apahce.catalina.Service接口的类.默认为org.apahce.catalina.core.StandardService</li>
<li>name定义Service的名字</li>
</ul>
</service></li>
</ul>
<h2 id="元素-2"><a href="#元素-2" class="headerlink" title="元素"></a><engine>元素</engine></h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;每个Service元素只能有一个Engine元素.元素处理在同一个<service>中所有<connector>元素接收到的客户请求.由org.apahce.catalina.Engine接口定义.</connector></service></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt; Engine name =<span class="string">"Catalina"</span> defaultHost =<span class="string">"localhost"</span> debug =<span class="string">"0"</span> &gt;</div></pre></td></tr></table></figure>
<ul>
<li>className指定实现Engine接口的类,默认值为StandardEngine</li>
<li>defaultHost指定处理客户的默认主机名,在<engine>中的<host>子元素中必须定义这一主机</host></engine></li>
<li>name定义Engine的名字在</li>
<li>在<engine>可以包含如下元素<logger>, <realm>, <value>, <host></host></value></realm></logger></engine></li>
</ul>
<h2 id="元素-3"><a href="#元素-3" class="headerlink" title="元素"></a><host>元素</host></h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它由Host接口定义.一个Engine元素可以包含多个<host>元素.每个<host>的元素定义了一个虚拟主机.它包含了一个或多个Web应用.</host></host></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt; Host name =<span class="string">"localhost"</span> debug =<span class="string">"0"</span> appBase =<span class="string">"webapps"</span> unpackWARs =<span class="string">"true"</span> autoDeploy =<span class="string">"true"</span> &gt;</div></pre></td></tr></table></figure>
<ul>
<li>className指定实现Host接口的类.默认值为StandardHost</li>
<li>appBase指定虚拟主机的目录,可以指定绝对目录,也可以指定相对于<catalina_home>的相对目录.如果没有此项,默认为<catalina_home>/webapps</catalina_home></catalina_home></li>
<li>autoDeploy如果此项设为true,表示Tomcat服务处于运行状态时,能够监测appBase下的文件,如果有新有web应用加入进来,会自运发布这个WEB应用</li>
<li>unpackWARs如果此项设置为true,表示把WEB应用的WAR文件先展开为开放目录结构后再运行.如果设为false将直接运行为WAR文件</li>
<li>alias指定主机别名,可以指定多个别名</li>
<li>deployOnStartup如果此项设为true,表示Tomcat服务器启动时会自动发布appBase目录下所有的Web应用.如果Web应用中的server.xml没有相应的<context>元素,将采用Tomcat默认的Context</context></li>
<li>name定义虚拟主机的名字</li>
<li>在<host>元素中可以包含如下子元素:<logger>, <realm>, <value>, <context></context></value></realm></logger></host></li>
</ul>
<h2 id="元素-4"><a href="#元素-4" class="headerlink" title="元素"></a><context>元素</context></h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它由Context接口定义.是使用最频繁的元素.每个&lt;Context元素代表了运行在虚拟主机上的单个Web应用.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个<host>可以包含多个<context>元素.每个web应用有唯一的一个相对应的Context代表web应用自身.servlet容器为第一个web应用创建一个ServletContext对象.</context></host></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt; Context path =<span class="string">"/sample"</span> docBase =<span class="string">"sample"</span> debug =<span class="string">"0"</span> reloadbale =<span class="string">"true"</span> &gt;</div></pre></td></tr></table></figure>
<ul>
<li>className指定实现Context的类,默认为StandardContext类</li>
<li>path指定访问Web应用的URL入口,注意/myweb,而不是myweb了事</li>
<li>reloadable如果这个属性设为true,Tomcat服务器在运行状态下会监视在WEB-INF/classes和Web-INF/lib目录CLASS文件的改运.如果监视到有class文件被更新,服务器自重新加载Web应用</li>
<li>cookies指定是否通过Cookies来支持Session,默认值为true</li>
<li>useNaming指定是否支持JNDI,默认值为了true</li>
<li>在<context>元素中可以包含如下元素<br><logger>, <realm>, <resource>, <resourceparams></resourceparams></resource></realm></logger></context></li>
</ul>
<h2 id="元素-5"><a href="#元素-5" class="headerlink" title="元素"></a><connector>元素</connector></h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;由Connector接口定义.<connector>元素代表与客户程序实际交互的给件,它负责接收客户请求,以及向客户返回响应结果.</connector></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt; Connector port =<span class="string">"8080"</span> maxThread =<span class="string">"50"</span> minSpareThreads =<span class="string">"25"</span> maxSpareThread =<span class="string">"75"</span> </div><div class="line">enableLookups =<span class="string">"false"</span> redirectPort =<span class="string">"8443"</span> acceptCount =<span class="string">"100"</span> debug =<span class="string">"0"</span> </div><div class="line">connectionTimeout =<span class="string">"20000"</span> disableUploadTimeout =<span class="string">"true"</span> /&gt; </div><div class="line">&lt; Connection port =<span class="string">"8009"</span> enableLookups =<span class="string">"false"</span> redirectPort =<span class="string">"8443"</span> debug =<span class="string">"0"</span> </div><div class="line">protocol =<span class="string">"AJP/1.3"</span> /&gt;</div></pre></td></tr></table></figure>
<ul>
<li>第一个Connector元素定义了一个HTTP Connector,它通过8080端口接收HTTP请求;</li>
<li>第二个Connector元素定义了一个JD Connector,它通过8009端口接收由其它服务器转发过来的请求.</li>
<li>Connector元素共用属性<ul>
<li>className指定实现Connector接口的类</li>
<li>enableLookups如果设为true,表示支持域名解析,可以把IP地址解析为主机名.WEB应用中调用request.getRemoteHost方法返回客户机主机名.默认值为true</li>
<li>redirectPort指定转发端口.如果当前端口只支持non-SSL请求,在需要安全通信的场命,将把客户请求转发至SSL的redirectPort端口</li>
</ul>
</li>
<li>HttpConnector元素的属性<ul>
<li>className实现Connector的类</li>
<li>port设定Tcp/IP端口,默认值为8080,如果把8080改成80,则只要输入<a href="http://localhost" target="_blank" rel="external">http://localhost</a> 即可,因为TCP/IP的默认端口是80</li>
<li>address如果服务器有二个以上ip地址,此属性可以设定端口监听的ip地址.默认情况下,端口会监听服务器上所有的ip地址</li>
<li>bufferSize设定由端口创建的输入流的缓存大小.默认值为2048byte</li>
<li>protocol设定Http协议,默认值为HTTP/1.1</li>
<li>maxThreads设定在监听端口的线程的最大数目,这个值也决定了服务器可以同时响应客户请求的最大数目.默认值为200</li>
<li>acceptCount设定在监听端口队列的最大客户请求数量,默认值为10.如果队列已满,客户必须等待.</li>
<li>connectionTimeout定义建立客户连接超时的时间.如果为-1,表示不限制建立客户连接的时间</li>
</ul>
</li>
<li>JkConnector的属性<ul>
<li>className实现Connector的类</li>
<li>port设定AJP端口号</li>
<li>protocol必须设定为AJP/1.3</li>
</ul>
</li>
</ul>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/5. 四大Java EE容器(Tomcat、JBoss、Resin、Glassfish)之简单比较/">四大Java EE容器(Tomcat、JBoss、Resin、Glassfish)之简单比较</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在流行的Java EE容器有很多：Tomcat、JBoss、Resin、Glassfish等等。下面对这四种Java EE容器进行了一番简单的比对。 1. Tomcat是Apache鼎力支持的Java Web应用服务器，由于它优秀的稳定性以及丰富的文档资料，广泛的使用人群，从而在开源领域受到最广泛的</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;现在流行的Java EE容器有很多：Tomcat、JBoss、Resin、Glassfish等等。下面对这四种Java EE容器进行了一番简单的比对。</p>
<ol>
<li><p>Tomcat是Apache鼎力支持的Java Web应用服务器(注：<strong>servlet容器</strong>)，由于它优秀的稳定性以及丰富的文档资料，广泛的使用人群，从而在开源领域受到最广泛的青睐。</p>
</li>
<li><p>Jboss作为Java EE应用服务器，<strong>它不但是Servlet容器，而且是EJB容器</strong>，从而受到企业级开发人员的欢迎，从而弥补了Tomcat只是一个Servlet容器的缺憾。</p>
</li>
<li><p>Resin也仅仅是一个Servlet容器，然而由于它优秀的运行速度，使得它在轻量级Java Web领域备受喜爱，特别是在互联网Web服务领域，众多知名公司都采用其作为他们的Java Web应用服务器，譬如163、ku6等。</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在商用应用服务器里主要有：Weblogic、Websphere,其中Weblogic我也使用过很长一段时间，当时也只用其当Servlet容器，然而就在同等条件下，在性能及易用性等方面，要比Tomcat优秀很多。</p>
<ol>
<li>glassfish是Sun公司推出的Java EE服务器(<strong>Java EE容器</strong>)，一个比较活跃的开源社区，不断的通过社区的反馈来提高其的可用性，经过glassfish v1 glassfish v2 到今天的glassfish v3 ,它已经走向成熟。Glassfish是一个免费、开放源代码的应用服务，它实现了Java EE 5,Java EE 5 平台包括了以下最新技术：EJB 3.0、JSF 1.2、Servlet 2.5、JSP 2.1、JAX-WS 2.0、JAXB 2.0、 Java Persistence 1.0、Common Annonations 1.0、StAX 1.0等。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;支持集群，通过内存中会话状态复制，增强了部署体系结构的可用性与可伸缩性，它对集群有着很好的支持，可以简单到通过添加机器，就可轻松的提高网站的 带负载能力，在解析能力方面，它对html的吞吐能力与apache服务器不分上下，就是tomcat所不能比的，支持目录部署，热部署，解决了 tomcat对热部署能力的缺陷。在版本方面做的更加人性化，有开发时用的简化版，专门用于部署web项目的版本，还要完全符合j2ee标准的版本。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/4. j2ee、j2se、ejb、javabean、serverlet、jsp之间关系/">j2ee、j2se、ejb、javabean、serverlet、jsp之间关系</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;j2ee：这个东西代表两个不同的东西，一种是sun的一种服务器软件，注意是一种具体的软件，不是技术。另一种是泛指sun的java技术，包括jsp,servlet,javabean,ejb等。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;j2se:我所知道的，sun的jdk有专门for电子设备的版本，叫j2me,用来开发手机,pda等应用，j2se则是为计算机用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;javabean:通俗的讲，就是封装了好多function的类,用来被别的jsp,servlet等调用的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ejb: 更通俗的讲，可以让你的javabean分布在多台计算机上以供你调用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;servlet: 用来通过http协议与用户交互</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jsp: servlet的UI设计太差，jsp可以实现更方便的UI设计。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;struts: jsp把网页和代码混在一起，太乱，于是出了个struts可以帮你把jsp的UI和逻辑部分分开处理。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/3. tomcat配置和优化/">tomcat配置和优化</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tomcat 的安装步骤非常简单，但是还需要学会如何去做简单的配置。tomcat 默认启动的是 8080，如果想修改成 80，则需要修改 sercer.xml 文件：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># vim /usr/local/tomcat/conf/server.xml</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;找到</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/tomcat%E9%85%8D%E7%BD%AE%E5%92%8C%E4%BC%98%E5%8C%96/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/tomcat%E9%85%8D%E7%BD%AE%E5%92%8C%E4%BC%98%E5%8C%96/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;找到  下一行插入新的  内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">&lt;Host name=<span class="string">"www.123.com"</span> appBase=<span class="string">"/data/tomcatweb"</span></div><div class="line">unpackWARs=<span class="string">"false"</span> autoDeploy=<span class="string">"true"</span></div><div class="line">xmlValidation=<span class="string">"false"</span> xmlNamespaceAware=<span class="string">"false"</span>&gt;</div><div class="line">&lt;Context path=<span class="string">""</span> docBase=<span class="string">""</span> debug=<span class="string">"0"</span> reloadable=<span class="string">"true"</span> crossContext=<span class="string">"true"</span>/&gt;</div><div class="line">&lt;/Host&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后，重启 tomcat</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># service tomcat stop</span></div><div class="line">[root@192 src]<span class="comment"># service tomcat start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该脚本并不支持 restart ，所以只能先 stop 然后再 start 。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面测试 jsp 解析</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># vim /data/tomcatweb/111.jsp</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内容如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">&lt;html&gt;&lt;body&gt;&lt;center&gt;</div><div class="line">    Now time is: &lt;%=new java.util.Date()%&gt;</div><div class="line">&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后访问，看结果</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># curl -xlocalhost:80 www.123.com/111.jsp</span></div><div class="line">&lt;html&gt;&lt;body&gt;&lt;center&gt;</div><div class="line"> Now time is: Sat Apr 01 21:42:44 CST 2017</div><div class="line">&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Tomcat/2. tomcat 安装/">tomcat 安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Tomcat/">Tomcat</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<a href="http://tomcat.apache.org/" target="_blank" rel="external">tomcat 官网</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@192 ~]<span class="comment"># cd /usr/local/src</span></div><div class="line">[root@192 src]<span class="comment"># wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.0.M19/bin/apache-tomcat-9.0.0.M19.tar.gz</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># tar zxf apache-tomcat-9.0.0.M19.tar.gz </span></div><div class="line">[root@192 src]<span class="comment"># mv apache-tomcat-9.0.0.M19 /usr/local/tomcat</span></div><div class="line">[root@192 src]<span class="comment"># cp -pv /usr/local/tomcat/bin/catalina.sh /etc/init.d/tomcat</span></div><div class="line"><span class="string">"/usr/local/tomcat/bin/catalina.sh"</span> -&gt; <span class="string">"/etc/init.d/tomcat"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># vim /etc/init.d/tomcat</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从第二行加如下配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#chkconfig: 2345 63 37</span></div><div class="line"><span class="comment">#description: tomcat server init script</span></div><div class="line"><span class="comment">#Source Function Library</span></div><div class="line">./etc/init.d/<span class="built_in">functions</span></div><div class="line">JAVA_HOME=/usr/<span class="built_in">local</span>/jdk1.8.0_111</div><div class="line">CATALINA_HOME=/usr/<span class="built_in">local</span>/tomcat</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/tomcat%20%E5%AE%89%E8%A3%85/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存该文件，然后把 tomcat 加入服务列表里面。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># chmod 755 /etc/init.d/tomcat</span></div><div class="line">[root@192 src]<span class="comment"># chkconfig --add tomcat</span></div><div class="line">[root@192 src]<span class="comment"># chkconfig tomcat on</span></div><div class="line">[root@192 src]<span class="comment"># service tomcat start</span></div><div class="line">/etc/init.d/tomcat: line 5: ./etc/init.d/<span class="built_in">functions</span>: Permission denied</div><div class="line">Using CATALINA_BASE: /usr/<span class="built_in">local</span>/tomcat</div><div class="line">Using CATALINA_HOME: /usr/<span class="built_in">local</span>/tomcat</div><div class="line">Using CATALINA_TMPDIR: /usr/<span class="built_in">local</span>/tomcat/temp</div><div class="line">Using JRE_HOME: /usr/<span class="built_in">local</span>/jdk1.8.0_111</div><div class="line">Using CLASSPATH: /usr/<span class="built_in">local</span>/tomcat/bin/bootstrap.jar:/usr/<span class="built_in">local</span>/tomcat/bin/tomcat-juli.jar</div><div class="line">Tomcat started.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测是否启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@192 src]<span class="comment"># ps aux | grep tomcat</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在浏览器上输入 <a href="http://192.168.0.73:8080" target="_blank" rel="external">http://192.168.0.73:8080</a> 可以看到 tomcat 的欢迎页。</p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/5">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
